package com.cap.bts.crmdp.dealer.meal.dao.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.cap.bts.crmdp.dealer.meal.dao.IMealLineManageDao;
import com.cap.bts.crmdp.dealer.meal.domain.MeallineModel;
import com.cap.bts.crmdp.dealer.meal.dto.MealDefineDto;
import com.cap.bts.framework.common.dao.callback.NativeQueryBySqlJpaCallback;
import com.cap.bts.framework.common.dao.impl.BaseDaoImpl;
import com.cap.bts.framework.common.exception.AppException;
import com.cap.bts.framework.common.util.Utils;
import com.cap.bts.framework.common.util.WhereConditionGenerator;

@Repository("iMealLineManageDao")
public class MealLineManageDaoIMpl extends BaseDaoImpl<MeallineModel, Long> implements
                                                                           IMealLineManageDao {

    @Inject
    public MealLineManageDaoIMpl(@Value("com.cap.bts.crmdp.dealer.meal.domain.MeallineModel") Class<MeallineModel> clazz) {
        super(clazz);

    }

    /**
     * 新增套餐详细信息
     * 
     * @see com.cap.bts.crmdp.dealer.meal.dao.IMealLineManageDao#insertMealLineInfo(com.cap.bts.crmdp.dealer.meal.domain.MeallineModel)
     */
    @Override
    public MeallineModel insertMealLineInfo(MeallineModel meallineModel) throws AppException {
        return save(meallineModel);

    }

    /**
     * 修改套餐的详细信息
     * 
     * @see com.cap.bts.crmdp.dealer.meal.dao.IMealLineManageDao#updateMealLineInfo(com.cap.bts.crmdp.dealer.meal.dto.MealDefineDto)
     */
    @Override
    public boolean updateMealLineInfo(MealDefineDto dto) throws AppException {
        MeallineModel meallineModel = findById(dto.getId());
        meallineModel.setShortName(dto.getShortName());
        meallineModel.setNumberOfUse(dto.getNumberOfUse());
        meallineModel.setHasVerificationCode(dto.getHasVerificationCode());
        meallineModel.setTicketDefId(dto.getTicketDefId());

        update(meallineModel);
        return true;
    }

    /**
     * 根据套餐ID查询套餐详细信息
     * 
     * @see com.cap.bts.crmdp.dealer.meal.dao.IMealLineManageDao#getMeallineList(com.cap.bts.crmdp.dealer.meal.dto.MealDefineDto)
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Object[]> getMeallineList(MealDefineDto dto) throws AppException {
        String select = "select ";
        String count = "  count(1) from (";
        String hql = "m.id,m.PACKAGE_DEF_ID,m.SHORT_NAME,m.DESCRIPTION,m.NUMBER_OF_USE,m.HAS_VERIFICATION_CODE,m.TICKET_DEF_ID,t.TICKET_TYPE ";
        String fromStr = " from TM_RAW_PACKAGE_REDEMPTION_LINES m left join TM_RAW_TICKET_DEF t on m.TICKET_DEF_ID=t.ID left join TM_CRM_PACKAGE_DEFINITION p on m.PACKAGE_DEF_ID=p.ID";
        String whereStr = " where 1=1 ";
        String orderHql = " order by m.ID desc";

        StringBuilder sqlStr = new StringBuilder();
        StringBuilder countStr = new StringBuilder();

        WhereConditionGenerator condition = new WhereConditionGenerator(whereStr);

        if (Utils.notNullAndZero(dto.getId())) {
            condition.and("m.PACKAGE_DEF_ID", "=", dto.getId());
        }

        sqlStr.append(select);
        sqlStr.append(hql);
        sqlStr.append(fromStr);
        sqlStr.append(condition.toString());
        sqlStr.append(orderHql);

        countStr.append(select);
        countStr.append(count);
        countStr.append(select);
        countStr.append(hql);
        countStr.append(fromStr);
        countStr.append(condition.toString());
        countStr.append(" ) alias");
        
        
        return getJpaTemplate().executeFind(new NativeQueryBySqlJpaCallback<Object[]>(sqlStr.toString()));
    }

    /**
     * 根据套餐详细信息的ID查询套餐详细信息
     * 
     * @see com.cap.bts.crmdp.dealer.meal.dao.IMealLineManageDao#getMealLIneInfo(java.lang.Long)
     */
    @Override
    public MeallineModel getMealLIneInfo(Long id) throws AppException {
        String sqlString = "select * from tm_raw_package_redemption_lines where id=?1";
        MeallineModel meallineModel = null;

        List<MeallineModel> meallineList = super.getListByNativeQueryWithVaParam(sqlString, id);
        if (!meallineList.isEmpty()) {
            meallineModel = meallineList.get(0);
        }
        return meallineModel;

    }

    /**
     * 根据套餐的ID查询套餐详细信息
     * 
     * @see com.cap.bts.crmdp.dealer.meal.dao.IMealLineManageDao#getMealLIneInfoByPackageId(java.lang.Long)
     */
    @Override
    public List<MeallineModel> getMealLIneInfoByPackageId(Long packageDefId) throws AppException {
        String sqlString = "select * from tm_raw_package_redemption_lines where package_def_id=?1";

        List<MeallineModel> meallineList = super.getListByNativeQueryWithVaParam(sqlString,
            packageDefId);
        if (!meallineList.isEmpty()) {
            return meallineList;
        }
        return new ArrayList<MeallineModel>();
    }

    /**
     * 根据套餐ID删除套餐详细信息
     * 
     * @see com.cap.bts.crmdp.dealer.meal.dao.IMealLineManageDao#removeMealLineByPackageDefId(java.lang.Long)
     */
    @SuppressWarnings("rawtypes")
    @Override
    public void removeMealLineByPackageDefId(List<MeallineModel> meallineList) throws AppException {
        for (Iterator iterator = meallineList.iterator(); iterator.hasNext();) {
            MeallineModel meallineModel = (MeallineModel) iterator.next();
            remove(meallineModel);
        }
    }

    /**
     * 根据优惠券和提货券的ID查询套餐详细信息
     * @see com.cap.bts.crmdp.dealer.meal.dao.IMealLineManageDao#getMeallineListByTicketDefId(java.lang.Long)
     */
    @Override
    public List<MeallineModel> getMeallineListByTicketDefId(Long ticketDefId) throws AppException {
        String sqlString = "select * from tm_raw_package_redemption_lines where ticket_def_id=?1";
        return super.getListByNativeQueryWithVaParam(sqlString, ticketDefId);
    }
}
